import {Component, Injector, OnInit} from '@angular/core';

import {AppComponentBase} from '@shared/app-component-base';

import {UploadDataComponent} from './../map/upload-data.component';

import {WaterDetectionCriterionServiceProxy} from '@shared/service-proxies/service-proxies';

@Component({selector: 'app-wdc-list', templateUrl: './wdc-list.component.html', styles: []})
export class WdcListComponent extends AppComponentBase implements OnInit {

    constructor(injector : Injector, public wdcService : WaterDetectionCriterionServiceProxy) {
        super(injector);
    }

    dataTable = {
        pageIndex: 1,
        pageSize: 10,
        total: 200,
        list: []
    };

    ngOnInit() {
        this.load();
    }

    upload() {

        this.showModal(UploadDataComponent, {
            title: '水样标准',
            action: this.uploadData,
            host: this
        }, () => {
             this.load();
        });

    }

    uploadData(file, host) : Promise < any > {

        return host
            .wdcService
            .uploadData(file)
            .toPromise();

    }

    load(pi?: number) {
        if (typeof pi !== 'undefined') {
            this.dataTable.pageIndex = pi || 1;
        }

        this.getDatas();
    }

    getDatas(){

        this.loading = true;
        this.wdcService.getCriterions(
            '',
            '',
            this.dataTable.pageSize,
            (this.dataTable.pageIndex - 1) * this.dataTable.pageSize)
            .finally(()=>{
                this.loading =false;
            })
            .subscribe((res)=>{
                console.log(res);
                this.dataTable.list = res.items;
                this.dataTable.total = res.totalCount;
            });

    }

}
